﻿using EMS.system.EntityAppService.WarehouseManagement.WarehouseSetup;
using EMS.system.IEntityAppService.WarehouseManagement.MaterialRequisition;
using EMS.system.IEntityAppService.WarehouseManagement.SalesReturns;
using EMS.system.IEntityAppService.WarehouseManagement.WarehouseSetup;
using EMS.system.IEntityAppService.WarehouseManagement.WarehouseSetup.WarehouseSetupDTO;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace EMS.system.Web.Controllers.WarehouseManagement.SalesReturns
{
    public class SalesReturnsController : Controller
    {
        private readonly ISalesReturnsAppService _salesReturnsAppService;

        public SalesReturnsController(ISalesReturnsAppService salesReturnsAppService)
        {
            _salesReturnsAppService = salesReturnsAppService;
        }


        public IActionResult Add()
        {
            return View();
        }

        public IActionResult SalesReturnsList()
        {
            return View();
        }

        public IActionResult Edit()
        {
            return View();
        }


        [HttpGet]
        public JsonResult GetSalesReturns(string? code, string? name, int page = 1, int limit = 2)
        {
            var list = _salesReturnsAppService.GetSalesReturns(code, name, page, limit);
            return Json(new { data = list.Queryable, count = list.RowCount, code = "0", msg = "" });
        }

        [HttpPost]
        public async Task<int> AddSalesReturns(SalesReturnsAppServiceDTO dto)
        {
            var result = await _salesReturnsAppService.AddSalesReturns(dto);
            return result;
        }



        [HttpPost]
        public async Task<int> DeleteSalesReturns(int id)
        {
            var result = await _salesReturnsAppService.DeleteSalesReturns(id);
            return result;
        }

        [HttpGet]
        public JsonResult GetSalesReturnsById(int id)
        {
            var result = _salesReturnsAppService.GetSalesReturnsById(id);
            return Json(new { data = result, code = "0", msg = "" });
        }
        [HttpPost]
        public async Task<int> UpdateSalesReturns(SalesReturnsAppServiceDTO dto)
        {
            var list = await _salesReturnsAppService.UpdateSalesReturns(dto);
            return list;
        }

        [HttpPost]
        public async Task<int> DeleAll(string id)
        {
            int res = 0;
            foreach (var ids in id.Split(","))
            {
                res = await _salesReturnsAppService.DeleteSalesReturns(int.Parse(ids));
            }
            return res;
        }
    }
}
